{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# coding=utf-8\n",
    "from __future__ import print_function, absolute_import\n",
    "\n",
    "import pandas as pd\n",
    "from gm.api import *\n",
    "import numpy as np\n",
    "import empyrical\n",
    "import sys\n",
    "\n",
    "sys.path.append(r\"../../../../\")\n",
    "from python_quant.stock.myTT import HHV, BARSLAST, IF, CROSS\n",
    "from python_quant.stock.MYTT_EXT import REF\n",
    "from python_quant.common.date import is_same_day"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以直接提取数据，掘金终端需要打开，接口取数是通过网络请求的方式，效率一般，行情数据可通过subscribe订阅方式\n",
    "设置token， 查看已有token ID,在用户-密钥管理里获取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "set_token('883a7fc43696f3cc100c78b423357976b2ec3352')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "获取历史数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "           symbol  open  close  high   low                       eob\n0     SZSE.300322 14.30  13.99 14.35 13.52 2012-06-08 00:00:00+08:00\n1     SZSE.300322 13.80  14.49 14.56 13.72 2012-06-11 00:00:00+08:00\n2     SZSE.300322 14.20  13.90 14.28 13.81 2012-06-12 00:00:00+08:00\n3     SZSE.300322 13.95  14.25 14.38 13.81 2012-06-13 00:00:00+08:00\n4     SZSE.300322 14.18  14.64 15.05 14.13 2012-06-14 00:00:00+08:00\n...           ...   ...    ...   ...   ...                       ...\n2341  SZSE.300322  8.40   8.59  8.96  8.30 2023-02-17 00:00:00+08:00\n2342  SZSE.300322  8.48   8.56  8.57  8.18 2023-02-20 00:00:00+08:00\n2343  SZSE.300322  8.80   9.71  9.80  8.80 2023-02-21 00:00:00+08:00\n2344  SZSE.300322 10.19  10.22 10.66  9.93 2023-02-22 00:00:00+08:00\n2345  SZSE.300322 10.06  10.08 10.65 10.00 2023-02-23 00:00:00+08:00\n\n[2346 rows x 6 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>symbol</th>\n      <th>open</th>\n      <th>close</th>\n      <th>high</th>\n      <th>low</th>\n      <th>eob</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>SZSE.300322</td>\n      <td>14.30</td>\n      <td>13.99</td>\n      <td>14.35</td>\n      <td>13.52</td>\n      <td>2012-06-08 00:00:00+08:00</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>SZSE.300322</td>\n      <td>13.80</td>\n      <td>14.49</td>\n      <td>14.56</td>\n      <td>13.72</td>\n      <td>2012-06-11 00:00:00+08:00</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>SZSE.300322</td>\n      <td>14.20</td>\n      <td>13.90</td>\n      <td>14.28</td>\n      <td>13.81</td>\n      <td>2012-06-12 00:00:00+08:00</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>SZSE.300322</td>\n      <td>13.95</td>\n      <td>14.25</td>\n      <td>14.38</td>\n      <td>13.81</td>\n      <td>2012-06-13 00:00:00+08:00</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>SZSE.300322</td>\n      <td>14.18</td>\n      <td>14.64</td>\n      <td>15.05</td>\n      <td>14.13</td>\n      <td>2012-06-14 00:00:00+08:00</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>2341</th>\n      <td>SZSE.300322</td>\n      <td>8.40</td>\n      <td>8.59</td>\n      <td>8.96</td>\n      <td>8.30</td>\n      <td>2023-02-17 00:00:00+08:00</td>\n    </tr>\n    <tr>\n      <th>2342</th>\n      <td>SZSE.300322</td>\n      <td>8.48</td>\n      <td>8.56</td>\n      <td>8.57</td>\n      <td>8.18</td>\n      <td>2023-02-20 00:00:00+08:00</td>\n    </tr>\n    <tr>\n      <th>2343</th>\n      <td>SZSE.300322</td>\n      <td>8.80</td>\n      <td>9.71</td>\n      <td>9.80</td>\n      <td>8.80</td>\n      <td>2023-02-21 00:00:00+08:00</td>\n    </tr>\n    <tr>\n      <th>2344</th>\n      <td>SZSE.300322</td>\n      <td>10.19</td>\n      <td>10.22</td>\n      <td>10.66</td>\n      <td>9.93</td>\n      <td>2023-02-22 00:00:00+08:00</td>\n    </tr>\n    <tr>\n      <th>2345</th>\n      <td>SZSE.300322</td>\n      <td>10.06</td>\n      <td>10.08</td>\n      <td>10.65</td>\n      <td>10.00</td>\n      <td>2023-02-23 00:00:00+08:00</td>\n    </tr>\n  </tbody>\n</table>\n<p>2346 rows × 6 columns</p>\n</div>"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "history_kline_data = history(symbol='SZSE.300322', frequency='1d', start_time='1991-01-01 09:00:00', end_time='2023-02-24 16:00:00', adjust=None, adjust_end_time='2023-02-24',fields='symbol, open, close, high, low, eob', df=True)\n",
    "history_kline_data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "查询当前行情快照，返回tick数据。\n",
    "实时模式，返回当前最新tick数据，\n",
    "回测模式，返回回测当前时间点的最近一分钟的收盘价\n",
    "直接接口数据提取返回实时模式的结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "[{'symbol': 'SHSE.600000', 'open': 7.22, 'high': 7.26, 'low': 7.18, 'price': 7.2, 'quotes': [{'bid_p': 7.19, 'bid_v': 48300, 'ask_p': 7.2, 'ask_v': 204100}, {'bid_p': 7.18, 'bid_v': 602256, 'ask_p': 7.21, 'ask_v': 153200}, {'bid_p': 7.17, 'bid_v': 566900, 'ask_p': 7.22, 'ask_v': 126100}, {'bid_p': 7.16, 'bid_v': 830200, 'ask_p': 7.23, 'ask_v': 229200}, {'bid_p': 7.15, 'bid_v': 638900, 'ask_p': 7.24, 'ask_v': 176600}], 'cum_volume': 10490108, 'cum_amount': 75619032.0, 'created_at': datetime.datetime(2023, 2, 24, 11, 31, 34, 419000, tzinfo=tzfile('PRC'))}]"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "current_data = current(symbols='SHSE.600000')\n",
    "current_data"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "统计分析"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "0.0"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "closes = history_kline_data['close'].values\n",
    "\n",
    "returns = np.array([1,10, 2, 4, 30, 1])\n",
    "\n",
    "# 计算最大回撤\n",
    "down = empyrical.max_drawdown(returns)\n",
    "down"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "           symbol  open close  high   low                       eob\n0     SZSE.300322 14.30 13.99 14.35 13.52 2012-06-08 00:00:00+08:00\n1     SZSE.300322 13.80 14.49 14.56 13.72 2012-06-11 00:00:00+08:00\n2     SZSE.300322 14.20 13.90 14.28 13.81 2012-06-12 00:00:00+08:00\n3     SZSE.300322 13.95 14.25 14.38 13.81 2012-06-13 00:00:00+08:00\n4     SZSE.300322 14.18 14.64 15.05 14.13 2012-06-14 00:00:00+08:00\n...           ...   ...   ...   ...   ...                       ...\n2342  SZSE.300322  8.48  8.56  8.57  8.18 2023-02-20 00:00:00+08:00\n2343  SZSE.300322  8.80  9.71  9.80  8.80 2023-02-21 00:00:00+08:00\n2344  SZSE.300322 10.19 10.22 10.66  9.93 2023-02-22 00:00:00+08:00\n2345  SZSE.300322 10.06 10.08 10.65 10.00 2023-02-23 00:00:00+08:00\n2346  SZSE.300322 10.09  9.62 10.15  9.56 2023-02-24 11:30:03+08:00\n\n[2347 rows x 6 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>symbol</th>\n      <th>open</th>\n      <th>close</th>\n      <th>high</th>\n      <th>low</th>\n      <th>eob</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>SZSE.300322</td>\n      <td>14.30</td>\n      <td>13.99</td>\n      <td>14.35</td>\n      <td>13.52</td>\n      <td>2012-06-08 00:00:00+08:00</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>SZSE.300322</td>\n      <td>13.80</td>\n      <td>14.49</td>\n      <td>14.56</td>\n      <td>13.72</td>\n      <td>2012-06-11 00:00:00+08:00</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>SZSE.300322</td>\n      <td>14.20</td>\n      <td>13.90</td>\n      <td>14.28</td>\n      <td>13.81</td>\n      <td>2012-06-12 00:00:00+08:00</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>SZSE.300322</td>\n      <td>13.95</td>\n      <td>14.25</td>\n      <td>14.38</td>\n      <td>13.81</td>\n      <td>2012-06-13 00:00:00+08:00</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>SZSE.300322</td>\n      <td>14.18</td>\n      <td>14.64</td>\n      <td>15.05</td>\n      <td>14.13</td>\n      <td>2012-06-14 00:00:00+08:00</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>2342</th>\n      <td>SZSE.300322</td>\n      <td>8.48</td>\n      <td>8.56</td>\n      <td>8.57</td>\n      <td>8.18</td>\n      <td>2023-02-20 00:00:00+08:00</td>\n    </tr>\n    <tr>\n      <th>2343</th>\n      <td>SZSE.300322</td>\n      <td>8.80</td>\n      <td>9.71</td>\n      <td>9.80</td>\n      <td>8.80</td>\n      <td>2023-02-21 00:00:00+08:00</td>\n    </tr>\n    <tr>\n      <th>2344</th>\n      <td>SZSE.300322</td>\n      <td>10.19</td>\n      <td>10.22</td>\n      <td>10.66</td>\n      <td>9.93</td>\n      <td>2023-02-22 00:00:00+08:00</td>\n    </tr>\n    <tr>\n      <th>2345</th>\n      <td>SZSE.300322</td>\n      <td>10.06</td>\n      <td>10.08</td>\n      <td>10.65</td>\n      <td>10.00</td>\n      <td>2023-02-23 00:00:00+08:00</td>\n    </tr>\n    <tr>\n      <th>2346</th>\n      <td>SZSE.300322</td>\n      <td>10.09</td>\n      <td>9.62</td>\n      <td>10.15</td>\n      <td>9.56</td>\n      <td>2023-02-24 11:30:03+08:00</td>\n    </tr>\n  </tbody>\n</table>\n<p>2347 rows × 6 columns</p>\n</div>"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "current_data = current(symbols='SZSE.300322')\n",
    "last_tick_date = current_data[0].created_at\n",
    "last_hist_date = history_kline_data.loc[len(history_kline_data) - 1].eob\n",
    "if is_same_day(last_tick_date, last_hist_date) is False:\n",
    "    df = pd.DataFrame(\n",
    "        [current_data[0]['symbol'], current_data[0]['open'], current_data[0]['price'], current_data[0]['high'],\n",
    "         current_data[0]['low'], current_data[0]['created_at']]\n",
    "    ).T\n",
    "    df.columns = history_kline_data.columns\n",
    "    history_kline_data = pd.concat([history_kline_data, df], axis=0, ignore_index=True)\n",
    "history_kline_data"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "分析"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "array([13.99, 14.49, 13.9, ..., 10.22, 10.08, 9.62], dtype=object)"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "# 取收盘价序列\n",
    "C = history_kline_data['close'].values\n",
    "\n",
    "# 取开盘价序列\n",
    "O = history_kline_data['open'].values\n",
    "\n",
    "# 取最高价序列\n",
    "H = history_kline_data['high'].values\n",
    "\n",
    "# 取最低价序列\n",
    "L = history_kline_data['low'].values\n",
    "\n",
    "X=HHV(H,5)\n",
    "X = np.nan_to_num(X, nan=-1)\n",
    "V=REF(X,1)\n",
    "V = np.nan_to_num(V, nan=-1)\n",
    "VAR111=C>V\n",
    "VAR222=BARSLAST(VAR111)\n",
    "D=REF(L,1)\n",
    "D = np.nan_to_num(D, nan=-1)\n",
    "B=IF(C>L,L,D)\n",
    "A=REF(B,VAR222)\n",
    "G=IF(D>L,L,D)\n",
    "OUTLINE = IF(VAR111,G,A)\n",
    "S = CROSS(OUTLINE,C)\n",
    "B = CROSS(C,OUTLINE)\n",
    "C"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "array([-1.  , -1.  , -1.  , ...,  9.8 , 10.66, 10.66])"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "V"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
